define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'mall/shop/settlement/index',
                    table: 'mall_shop_settlement',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'settlementId',
                sortName: 'settlementId',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'settlementId', title: __('Settlementid'), visible:false},
                        {field: 'settlementNo', title: __('Settlementno')},
                        {field: 'settlementType', title: __('Settlementtype'), searchList: {"1":__('Settlementtype 1'),"0":__('Settlementtype 0')}, formatter: Table.api.formatter.normal},
                        {field: 'accName', title: __('Accname'), visible:false},
                        {field: 'accNo', title: __('Accno'), visible:false},
                        {field: 'accUser', title: __('Accuser'), visible:false},
                        {field: 'areaName', title: __('Areaname'), visible:false},
                        {field: 'settlementMoney', title: __('Settlementmoney'), operate:'BETWEEN'},
                        {field: 'commissionFee', title: __('Commissionfee'), operate:'BETWEEN'},
                        {field: 'backMoney', title: __('Backmoney'), operate:'BETWEEN'},
                        {field: 'settlementStatus', title: __('Settlementstatus'), searchList: {"1":__('Settlementstatus 1'),"0":__('Settlementstatus 0')}, formatter: Table.api.formatter.status},
                        {field: 'settlementTime', title: __('Settlementtime'), operate:'RANGE', addclass:'datetimerange'},
                        {field: 'remarks', title: __('Remarks')},
                        {field: 'createtime', title: __('Create time'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime, visible:false},
                        {field: 'operate', title: __('Operate'), table: table,events: Table.api.events.operate,
                            buttons: [{
                                name: 'detail',
                                text: __('Detail'),
                                classname: 'btn btn-xs btn-success btn-fields btn-dialog',
                                icon: 'fa fa-list',
                                url: 'mall/shop/settlement/detail'
                            },{
                                name: 'settlement',
                                text: __('Settlement'),
                                classname: 'btn btn-xs btn-info btn-dialog',
                                icon: 'fa fa-th-list',
                                visible: function(row){
                                    return row.settlementStatus==0;
                                },
                                url: 'mall/shop/settlement/handle',
                                refresh: $('#toolbar>.btn-refresh').trigger("click")
                            }
                            ],
                            formatter: Table.api.formatter.buttons
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        handle: function () {
            Controller.api.bindevent();
            var settlement_id = $("#c-settlementId").val();
            Controller.api.bindTable(settlement_id);
        },
        detail: function () {
            Controller.api.bindevent();
            var settlement_id = $("#c-settlementId").val();
            Controller.api.bindTable(settlement_id);
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            },
            bindTable:function(settlement_id){
                Table.api.init({
                    extend: {
                        detail_url: 'mall/shop/settlement/detail/ids/'+settlement_id,
                        table: 'mall_shop_settlement',
                    }
                });
                var table = $("#table");
                // 初始化表格
                table.bootstrapTable({
                    url: $.fn.bootstrapTable.defaults.extend.detail_url,
                    pk: 'id',
                    sortName: 'id',
                    columns: [
                        [
                            {field: 'orderNo', title: __('Orderno')},
                            {field: 'payType', title: __('Paytype'),searchList: {"0":__('Paytype 0'),"1":__('Paytype 1')}, formatter: Table.api.formatter.normal},
                            {field: 'goodsMoney', title: __('Goodsmoney'), operate:'BETWEEN'},
                            {field: 'deliverMoney', title: __('Delivermoney'), operate:'BETWEEN'},
                            {field: 'totalMoney', title: __('Totalmoney'), operate:'BETWEEN'},
                            {field: 'realTotalMoney', title: __('Realtotalmoney'), operate:'BETWEEN'},
                            {field: 'commissionFee', title: __('Commissionfee'), operate:'BETWEEN'},
                            {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime, visible:false},
                        ]
                    ],
                    search: false,
                    commonSearch:false,
                    showExport: false,
                    showColumns: false,
                    showToggle: false,
                    pagination: false
                });
                // 为表格绑定事件
                Table.api.bindevent(table);

            },
        }
    };
    return Controller;
});